// 用户相关的数据库操作
import { executeSql } from '../util/db.js';

// 获取管理员用户信息
export const getAdminUser = function() {
    let sql = `SELECT id, username, nickname, email, userPic FROM admin_users WHERE username = ?`;
    return executeSql(sql, ['admin']);
}

// 更新管理员用户信息
export const updateAdminUser = function(nickname, email, userPic) {
    let sql = `UPDATE admin_users SET nickname = ?, email = ?, userPic = ? WHERE username = ?`;
    return executeSql(sql, [nickname, email, userPic, 'admin']);
}

// 初始化管理员用户（如果不存在）
export const initAdminUser = function() {
    let sql = `INSERT IGNORE INTO admin_users (username, password, nickname, email, userPic) VALUES (?, ?, ?, ?, ?)`;
    return executeSql(sql, ['admin', '123456', '管理员', 'admin@example.com', '/uploads/admin-avatar.jpg']);
}

// 检查用户表是否存在，如果不存在则创建
export const createAdminUsersTable = function() {
    let sql = `
        CREATE TABLE IF NOT EXISTS admin_users (
            id INT(11) NOT NULL AUTO_INCREMENT,
            username VARCHAR(50) NOT NULL UNIQUE,
            password VARCHAR(255) NOT NULL,
            nickname VARCHAR(100) NOT NULL,
            email VARCHAR(100) NOT NULL,
            userPic VARCHAR(255) DEFAULT '/uploads/admin-avatar.jpg',
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (id)
        ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
    `;
    return executeSql(sql);
}
